package model.dao;

import java.util.List;

import javax.ejb.Local;
import javax.ejb.Stateless;
import javax.persistence.NoResultException;
import javax.persistence.Query;

import model.entity.Tag;

@Stateless
@Local(TagDaoLocal.class)
public class TagDaoBean extends GenericDaoBean<Tag, Integer> implements
		TagDaoLocal {

	@Override
	public List<Tag> findByContent(String content) {
		try{
			Query q = em.createQuery("SELECT tag FROM Tag tag WHERE tag.text=:content");
			q.setParameter("content", content);
			return  q.getResultList();
		}catch(NoResultException e){
			return null;
		}
	}

	@Override
	public List<Tag> findByContentA(String content) {
		try{
			Query q = em.createQuery("SELECT tag FROM Tag tag WHERE tag.text=:content");
			q.setParameter("content", "%"+content+"%");
			return  q.getResultList();
		}catch(NoResultException e){
			return null;
		}
	}
	@Override
	public int removeRelationshipTagPhoto(Integer tagId, Integer photoId) {
		try{
			Query q = em.createNativeQuery("DELETE FROM tags_on_photo WHERE tag_id=? AND photo_id=?");
			q.setParameter(1, tagId);
			q.setParameter(2, photoId);
			return  q.executeUpdate();
		}catch(NoResultException e){
			return -1;
		}
	}

}
